var api = require('../../utils/api.js')
var $ = require('../../utils/util.js')
var regeneratorRuntime = require('../../utils/runtime.js')
var app = getApp()
var Store = app.Store
Page({

  /**
   * 页面的初始数据
   */
  data: {
      key:"A9D.8F.7G6.H5J4.K3I.251Q.W5F6.G4H6.KJ8L5;9'68JDAS.ODKJS.9KD.SAM.K893.212.131.56",
      meterId:"",
      meterName:"",
      companyName:"",
      projectName:"",
      rateId:"",
      rateName:"",
      propertyId:"",
      propertyName:"",
      userId1:"",
      userId2:"",
      userName1:"",
      userName2:"",
      companyList:[],
      projectList:[],
      rateList:[],
      meterList:[],
      propertyList:[],
      userList:[],
      isShowPop_rate:false,
      isShowPop_property:false,
      isShowPop_project:false,
      isShowPop_company:false,
      isShowPop_user1:false,
      isShowPop_user2:false,
      keyword:null,
      isShow_recharge:false,
      rechargeValue:"",
      isShow_refund:false,
      refundValue:"",
      coefficient:1.0,
      isShow_reset:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.getCompanyList();
    this.getProjectList();
    this.getPropertyList();
    this.getRateList();
    this.getUserList();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  scanCode(){
    var that=this;
    wx.scanCode({
      success (res) {
        if(res!=null && res.result!=null){
          if(res.result.indexOf("?id=")!=-1){
            console.log(res.result.substr(res.result.lastIndexOf("?id=")+4))
            that.setData({
              "meterId":res.result.substr(res.result.lastIndexOf("?id=")+4) || "",
              "meterName":"",
              "meterData":"",
              "userId1":"",
              "userId2":"",
              "userName1":"",
              "userName2":""
            })
            var params={
              "key":that.data.key,
              "meterId":that.data.meterId,
            }
            api.$get(api.meter.meterInfo, params).then((res) => { 
              console.log(res)
              if(res!=null && res.data!=null && res.data.id!=null){
                $.toast('电表已录入')
                that.setData({
                  "meterId":res.data.id || "",
                  "meterName":res.data.name|| "",
                  "companyName":res.data.company|| "",
                  "projectName":res.data.project|| "",
                  "rateId":res.data.rateId|| "",
                  "rateName":res.data.rateName|| "",
                  "propertyId":res.data.propertyId || "",
                  "propertyName":res.data.propertyName ||"",
                  "userId1":res.data.userId1||"",
                  "userName1":res.data.userName1||"",
                  "userId2":res.data.userId2||"",
                  "userName2":res.data.userName2||"",
                  "meterData":res.data.meterData||"",
                  "coefficient":res.data.coefficient||"",
                })
              }else{
                if(res.data.meterData!=null && res.data.meterData!=''){
                  that.setData({
                    "meterData":res.data.meterData||"",
                  })
                }
                $.toast('电表尚未录入')
              }
            })
          }else{
            $.toast('无法识别二维码')
            that.setData({
              "meterId":"",
              "meterName":"",
              "meterData":"",
              "userId1":"",
              "userId2":"",
              "userName1":"",
              "userName2":""
            })
          }
        }else{
          $.toast('无法识别二维码')
          that.setData({
            "meterId":"",
            "meterName":"",
            "meterData":"",
            "userId1":"",
            "userId2":"",
            "userName1":"",
            "userName2":""
          })
        }
      },
      fail(){
        $.toast('无法识别二维码')
        that.setData({
          "meterId":"",
          "meterName":"",
          "meterData":"",
          "userId1":"",
          "userId2":"",
          "userName1":"",
          "userName2":""
        })
      }
    })
  },
  selectCompanyHandle(){
      this.setData({
        "isShowPop_rate":false,
        "isShowPop_property":false,
        "isShowPop_project":false,
        "isShowPop_company":true
      })
  },
  selectProjectHandle(){
    this.setData({
      "isShowPop_rate":false,
      "isShowPop_property":false,
      "isShowPop_project":true,
      "isShowPop_company":false,
      "isShowPop_user1":false,
      "isShowPop_user2":false,
    })
  },
  selectPropertyHandle(){
    this.setData({
      "isShowPop_rate":false,
      "isShowPop_property":true,
      "isShowPop_project":false,
      "isShowPop_company":false,
      "isShowPop_user1":false,
      "isShowPop_user2":false,
    })
  },
  selectRateHandle(){
    this.setData({
      "isShowPop_rate":true,
      "isShowPop_property":false,
      "isShowPop_project":false,
      "isShowPop_company":false,
      "isShowPop_user1":false,
      "isShowPop_user2":false,
    })
  },
  selectUserHandle1(){
    this.setData({
      "isShowPop_rate":false,
      "isShowPop_property":false,
      "isShowPop_project":false,
      "isShowPop_company":false,
      "isShowPop_user1":true,
      "isShowPop_user2":false,
    })
  },
  selectUserHandle2(){
    this.setData({
      "isShowPop_rate":false,
      "isShowPop_property":false,
      "isShowPop_project":false,
      "isShowPop_company":false,
      "isShowPop_user1":false,
      "isShowPop_user2":true,
    })
  },
  add(){
    if ($.isNull(this.data.meterId)) {
      $.toast('请扫描电表上的二维码')
    }else if ($.isNull(this.data.companyName)) {
      $.toast('请选择公司')
    } else if ($.isNull(this.data.projectName)) {
      $.toast('请选择项目')
    } else if ($.isNull(this.data.propertyName) || $.isNull(this.data.propertyId)) {
      $.toast('请选择物业')
    }else if ($.isNull(this.data.rateId)) {
      $.toast('请选择收费标准')
    } else if ($.isNull(this.data.userId1)) {
      $.toast('请选择用户1')
    }else if ($.isNull(this.data.meterName)) {
      $.toast('请填写电表名称')
    }else if ($.isNull(this.data.coefficient)) {
      $.toast('请填写电表电表变比')
    }else if (/[a-zA-Z]/.test(this.data.coefficient+"")) {
      $.toast('电表变比必须为数字')
      this.setData({
        coefficient: ""
      })
    }else {
      var params = {
        key:this.data.key,
        meterId:this.data.meterId,
        rateId:this.data.rateId,
        company:this.data.companyName,
        project:this.data.projectName,
        propertyId:this.data.propertyId,
        meterName:this.data.meterName,
        merchant:  this.data.userId2==""?this.data.userId1:this.data.userId1+","+this.data.userId2,
        coefficient: this.data.coefficient
      }


      console.log(params);
      api.$get(api.meter.addMeter, params).then((res) => { 
        $.toast('提交成功')
      })
    } 
  },
  //##input 输入##
  input_handle(e) {

        this.setData({
          [e.currentTarget.dataset.key]: e.detail.value
        })
       

  },
  getCompanyList(){
    var params={
      "key":this.data.key,
    }
    console.log("getCompanyList")
    api.$get(api.meter.companyList, params).then((res) => { 
      console.log(res)
      if(res.data!=null){
        this.setData({
          "companyList":res.data
        })
        for(var i=0;i<res.data.length;i++){
          if(res.data[i].indexOf("中信")!=-1){
              this.setData({
                "companyName":res.data[i],
              })
          }
        }
      }
    })
  },
  getProjectList(){
    var params={
      "key":this.data.key,
    }
    api.$get(api.meter.projectList, params).then((res) => { 
      console.log(res)
      if(res.data!=null){
        this.setData({
          "projectList":res.data
        })
        for(var i=0;i<res.data.length;i++){
          if(res.data[i].indexOf("海卉")!=-1){
              this.setData({
                "projectName":res.data[i]
              })
          }
        }
      }
    })
  },
  getRateList(){
    var params={
      "key":this.data.key,
    }
    api.$get(api.meter.rateList, params).then((res) => { 
      console.log(res)
      if(res.data!=null){
        this.setData({
          "rateList":res.data
        })
        for(var i=0;i<res.data.length;i++){
          if(res.data[i].name.indexOf("海卉")!=-1){
              this.setData({
                "rateName":res.data[i].name,
                "rateId":res.data[i].id
              })
          }
        }
      }
    })
  },
  getMeterList(){
    var params={
      "key":this.data.key,
    }
    api.$get(api.meter.meterList, params).then((res) => { 
      console.log(res)
    
    })
  },
  getPropertyList(){
    var params={
      "key":this.data.key,
    }
    api.$get(api.meter.propertyList, params).then((res) => { 
      console.log(res)
      if(res.data!=null){
        this.setData({
          "propertyList":res.data
        })
        for(var i=0;i<res.data.length;i++){
          if(res.data[i].name.indexOf("海卉")!=-1){
              this.setData({
                "propertyName":res.data[i].name,
                "propertyId":res.data[i].id
              })
          }
        }
      }
    })
  },
  getUserList(){
    var params={
      "key":this.data.key,
      "phone":this.data.keyword==""?null:this.data.keyword,
    }
    api.$get(api.meter.userList, params).then((res) => { 
      console.log(res)
      if(res.data!=null){
        this.setData({
          "userList":res.data
        })
      }
    })
  },
  onClosePop_rate(){
    this.setData({
      "isShowPop_rate":false,
      "isShowPop_property":false,
      "isShowPop_project":false,
      "isShowPop_company":false,
      "isShowPop_user1":false,
      "isShowPop_user2":false,
    })
  },
  onClosePop_property(){
    this.setData({
      "isShowPop_rate":false,
      "isShowPop_property":false,
      "isShowPop_project":false,
      "isShowPop_company":false,
      "isShowPop_user1":false,
      "isShowPop_user2":false,
    })
  },
  onClosePop_project(){
    this.setData({
      "isShowPop_rate":false,
      "isShowPop_property":false,
      "isShowPop_project":false,
      "isShowPop_company":false,
      "isShowPop_user1":false,
      "isShowPop_user2":false,
    })
  },
  onClosePop_company(){
    this.setData({
      "isShowPop_rate":false,
      "isShowPop_property":false,
      "isShowPop_project":false,
      "isShowPop_company":false,
      "isShowPop_user1":false,
      "isShowPop_user2":false,
    })
  },
  onClosePop_user1(){
    this.setData({
      "isShowPop_rate":false,
      "isShowPop_property":false,
      "isShowPop_project":false,
      "isShowPop_company":false,
      "isShowPop_user1":false,
      "isShowPop_user2":false,
    })
  },
  onClosePop_user2(){
    this.setData({
      "isShowPop_rate":false,
      "isShowPop_property":false,
      "isShowPop_project":false,
      "isShowPop_company":false,
      "isShowPop_user1":false,
      "isShowPop_user2":false,
    })
  },
  popTabHandle_rate(e){
    this.setData({
      "rateId":e.currentTarget.dataset.id,
      "rateName":e.currentTarget.dataset.name,
    })
    this.onClosePop_rate();
  },
  popTabHandle_property(e){
    this.setData({
      "propertyId":e.currentTarget.dataset.id,
      "propertyName":e.currentTarget.dataset.name,
    })
    this.onClosePop_property();
  },
  popTabHandle_project(e){
    this.setData({
      "projectName":e.currentTarget.dataset.name,
    })
    this.onClosePop_project();
  },
  popTabHandle_company(e){
    this.setData({
      "companyName":e.currentTarget.dataset.name,
    })
    this.onClosePop_company();
  },
  popTabHandle_user1(e){
    this.setData({
      "userId1":e.currentTarget.dataset.id,
      "userName1":e.currentTarget.dataset.name+"("+e.currentTarget.dataset.id+")",
    })
    this.onClosePop_user1();
  },
  popTabHandle_user2(e){
    this.setData({
      "userId2":e.currentTarget.dataset.id,
      "userName2":e.currentTarget.dataset.name+"("+e.currentTarget.dataset.id+")",
    })
    this.onClosePop_user2();
  },
  keywordConfirmHandle(){
    this.getUserList()
  },
  refreshHandle(){
    var params={
      "key":this.data.key,
      "meterId":this.data.meterId,
    }
    api.$get(api.meter.meterInfo, params).then((res) => { 
      console.log(res)
      if(res!=null && res.data!=null && res.data.id!=null){
        $.toast('刷新成功,此电表已录入')

        this.setData({
          "meterId":res.data.id || "",
          "meterName":res.data.name|| "",
          "companyName":res.data.company|| "",
          "projectName":res.data.project|| "",
          "rateId":res.data.rateId|| "",
          "rateName":res.data.rateName|| "",
          "propertyId":res.data.propertyId || "",
          "propertyName":res.data.propertyName ||"",
          "userId1":res.data.userId1||"",
          "userName1":res.data.userName1||"",
          "userId2":res.data.userId2||"",
          "userName2":res.data.userName2||"",
          "meterData":res.data.meterData||"",
        })
      }
    })
  },
  resetHandle(){
    if($.isNull(this.data.meterId)){
      $.toast('请扫描电表上的二维码')
      return;
    }
      this.setData({
        "isShow_reset":true
      })
  },
  reset(){
    if($.isNull(this.data.meterId)){
      $.toast('请扫描电表上的二维码')
      return;
    }
    var params={
      "key":this.data.key,
      "meterId":this.data.meterId,
    }
    api.$get(api.meter.reset, params).then((res) => { 
        $.toast(res.data)
    })
    this.setData({
      "isShow_reset":false
    })
  },
  rechargeHandle(){
    if($.isNull(this.data.meterId)){
      $.toast('请扫描电表上的二维码')
      return;
    }
      this.setData({
        "isShow_recharge":true,
        "rechargeValue":""
      })
  },
  recharge(){
    console.log("recharge",this.data.rechargeValue)
    if($.isNull(this.data.meterId)){
      $.toast('请扫描电表上的二维码')
      return;
    }
    var val=parseFloat(this.data.rechargeValue)
    console.log(val)
    if(isNaN(val)){
      $.toast('无法识别您输入的金额')
      return
    }
    if(val<=0.0){
      $.toast('充值金额必须大于0')
      return
    }

    var params={
      "key":this.data.key,
      "meterId":this.data.meterId,
      "value":val,
      "type":"degree",//充值电度 or 充值金额  degree or money
    }
    api.$get(api.meter.recharge, params).then((res) => { 
      console.log(res)
      $.toast(res.data)
    })

    this.setData({
      "isShow_recharge":false,
      "rechargeValue":""
    })

  },
  refundHandle(){
    if($.isNull(this.data.meterId)){
      $.toast('请扫描电表上的二维码')
      return;
    }
      this.setData({
        "isShow_refund":true,
        "refundValue":""
      })
  },
  refund(){
    console.log("refund",this.data.refundValue)
    if($.isNull(this.data.meterId)){
      $.toast('请扫描电表上的二维码')
      return;
    }
    var val=parseFloat(this.data.refundValue)
    console.log(val)
    if(isNaN(val)){
      $.toast('无法识别您输入的金额')
      return
    }
    if(val<=0.0){
      $.toast('充值金额必须大于0')
      return
    }

    var params={
      "key":this.data.key,
      "meterId":this.data.meterId,
      "value":val,
      "type":"degree",//退款电度 or 退款金额  degree or money
    }
    api.$get(api.meter.refund, params).then((res) => { 
      console.log(res)
      $.toast(res.data)
    })

    this.setData({
      "isShow_refund":false,
      "refundValue":""
    })

  }




})